within BusinessSimulation.Converters.Lookup;

block TableFunction "Table function defined by external data on file using Modelica's CombiTable format"
  import BusinessSimulation.Units.*;
  import Modelica.Blocks.Types.{Extrapolation, Smoothness};
  import BusinessSimulation.Types.TimeBases;
  extends Interfaces.PartialConverters.SISO_Conversion;
  extends Icons.InterpolationTable;
  parameter Boolean tableOnFile = false "= true, if table is defined on file or in function usertab (combiTable1D.tableOnFile)" annotation(Dialog(group = "TableDataDefinition"));
  parameter Real table[:, :] = fill(0.0, 2, 2) "Table matrix (combiTable1D.table)" annotation(Dialog(group = "TableDataDefinition", enable = not tableOnFile));
  parameter String tableName = "NoName" "Name of the table in the file containing the data" annotation(Dialog(group = "TableDataDefinition", enable = tableOnFile));
  parameter String fileNameURI = modelSettings.lookupDataFileURI "URI of the file to read the data from (default = modelSettings.lookupDataFileURI)" annotation(Dialog(group = "TableDataDefinition", enable = tableOnFile));
  parameter Smoothness smoothness = Smoothness.LinearSegments "Smoothness of table interpolation" annotation(Dialog(group = "TableDataInterpretation"));
  parameter Integer column(min = 2) = 2 "Column of table to be interpolated" annotation(Dialog(group = "TableDataInterpretation"));
  // only one column is used in this component
  parameter Boolean verboseRead = true "= true, if info message that file is loading is to be printed (combiTable1D.verboseRead)" annotation(Dialog(group = "TableDataDefinition", enable = tableOnFile));
  parameter Extrapolation extrapolation = Modelica.Blocks.Types.Extrapolation.HoldLastPoint "Extrapolation of data outside the definition range (combiTable.extrapolation)" annotation(Dialog(group = "TableDataInterpretation"));
  parameter Boolean verboseExtrapolation = false "= true, if warning messages are to be printed if table input is outside the definition range (combiTable.verboseExtrapolation)" annotation(Dialog(group = "TableDataInterpretation"));
  outer ModelSettings modelSettings;
protected
  parameter String fileName = if tableOnFile then Modelica.Utilities.Files.loadResource(fileNameURI) else "NoName" "Absolute path to the file specified by fileName" annotation(Dialog(group = "Initialization", enable = false));
  Modelica.Blocks.Tables.CombiTable1Ds combiTable(fileName = fileName, tableName = tableName, smoothness = smoothness, tableOnFile = tableOnFile, table = table, columns = {column}, verboseRead = verboseRead, extrapolation = extrapolation, verboseExtrapolation = verboseExtrapolation) annotation(Placement(visible = true, transformation(origin = {0, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  assert(column >= 2 and column <= size(table, 2), "column is out of range");
  connect(combiTable.y[1], y_c) annotation(Line(visible = true, origin = {25.5, 0}, points = {{-14.5, 0}, {14.5, 0}}, color = {1, 37, 163}));
  connect(u_c, combiTable.u) annotation(Line(visible = true, origin = {-41, 0}, points = {{-29, 0}, {29, 0}}, color = {1, 37, 163}));
  annotation(Documentation(info = "<html>
<p class=\"aside\">This information is part of the Business Simulation&nbsp;Library (BSL). Please support this work and <a href=\"https://www.paypal.com/donate/?hosted_button_id=GXVZT8LD7CFXN\" style=\"font-weight:bold; color:orange; text-decoration:none;\">&#9658;&nbsp;donate</a>.</p>
<p>This converter uses the Modelica CombiTable format to read in a 1-D table function from file. For more explanation regarding the 1-D Modelica CombiTable interpolation see <a href=\"modelica:/Modelica.Blocks.Tables.CombiTable1Ds\">Modelica.Blocks.Tables.CombiTable1Ds</a>.</p>
<p>For more convenience, the input <strong>u</strong> to and the ouput from the lookup table can be converted into different units. This typically applies to <em>rates and times</em> which in non-technical modeling will typically not be referenced in SI-units (e.g., <code>s</code>).</p>
<h4>Notes</h4>
<p>The Modelica CombiTable format can be generated by Wolfram Mathematica using <code>Export[]:</code></p>
<p><code>Export[ <em>filename</em>, { {\"TableName1\", table1}, {\"TableName2\", table2}, ... }, \"MCTT\" ]</code></p>
<p>where <code>table1, table2, ...</code> are 2-dimensional arrays. By default all lookup data are assumed to be included in a single file whose name is given by the global parameter <code>lookupDataFileURI</code>.</p>
<h4>See also</h4>
<p><a href=\"modelica://BusinessSimulation.Converters.Lookup.TableFunctionVector\">TableFunctionVector</a>,&nbsp;<a href=\"modelica://BusinessSimulation.InformationSources.LinearTimeTable\">LinearTimeTable</a></p></html>", revisions = "<html>
<ul>
<li>Updated in v2.0.0.</li>
</ul>
</html>"), Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {10, 10})), Diagram(coordinateSystem(extent = {{-148.5, -105}, {148.5, 105}}, preserveAspectRatio = true, initialScale = 0.1, grid = {5, 5})));
end TableFunction;
